package com.inspinia.base.dao.pageable;

public class MySQLDialect extends Dialect {

    final static String LIMIT_SQL_PATTERN = "%s limit %s, %s";

    final static String LIMIT_SQL_PATTERN_FIRST = " %s limit %s";

    @Override
    public String getLimitString(String sql, int offset, int limit, String orderBy) {
        if (orderBy != null && !"".equals(orderBy)) {
            if (sql.indexOf("order by") > 1) {
                sql += "," + orderBy;
            } else {
                sql += " order by " + orderBy;
            }
        }
        if (offset == 0) {
            return String.format(LIMIT_SQL_PATTERN_FIRST, sql, limit);
        }

        return String.format(LIMIT_SQL_PATTERN, sql, offset, limit);
    }

}
